<?php
namespace App\Http\Controllers\Backend;

use Aitifen\Library\Xxtea\Xxtea;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

class UsersController extends Controller
{
    /**
     * 获取登录状态
     */
    public function getLoginStatus(Request $request)
    {
        //登录信息先从 cookie取，没有去header中去取
        $accessToken = array_key_exists('USER-ACCESS-TOKEN', $_COOKIE) ? $_COOKIE['USER-ACCESS-TOKEN'] : '';
        if(empty($accessToken)) {
            $accessToken = $request->header('USER-ACCESS-TOKEN');
        }

        try {

            // 获取平台信息(appkey|appsecret)
            $postData['agencyDomain'] = $request->header('host');
            $client = $this->getClient(STEAM_SERVICE_NAME, 'CommonService');
            $client->clearHeaders();
            $result = $client->getPlatformInfo($postData);

            // 将app_key|app_secret加密存储到cookie中
            $encryptString = Xxtea::encrypt(['app_key' => $result['app_key'], 'app_secret' => $result['app_secret']], Config::get('common.cookie_encrypt_key'));
            setcookie('app_data', $encryptString, time() + 86400, '/');
            unset($result['app_key'], $result['app_secret'], $result['platform_key'], $result['id'], $result['create_at'], $result['update_at'], $result['create_uid'], $result['update_uid']);
            //未登录
            if(!$accessToken) {
                $result['login_status'] = 0;
            } else {
                //登录
                $server = $this->getClient(STEAM_SERVICE_NAME, 'AuthService');
                $server->getUserData([]);
                $result['login_status'] = 1;
            }
        } catch (\Exception $e) {
            if ($e->getMessage() == '1001') {
                $result['login_status'] = 0;
            } else {
                return $this->returnJson(301, $e->getMessage());
            }
        }
        return $this->returnJson(0, '操作成功', $result);
    }

    /**
     *  设置用户登录
     */
    public function setToken(Request $request)
    {
        $token = trim($request->post('token'));
        $platform = trim($request->post('platform'));
//        $url      = trim($request->post('url'));
        try {
            $server = $this->getClient(STEAM_SERVICE_NAME, 'AuthService');
            $server->clearHeaders();
            $data = $server->setUserToken(['token' => $token, 'platformName' => $platform, 'type' => 'employee']);
            setcookie('USER-ACCESS-TOKEN', $data['accessToken'], time() + 86400, '/');
        } catch(\Exception $e) {
            return $this->returnJson(301, $e->getMessage());
        }

        return $this->returnJson(0, '登录成功', ['accessToken' => $data['accessToken']]);
    }

    /**
     *  退出登录
     */
    public function logout(Request $request)
    {
        try {
            setcookie('USER-ACCESS-TOKEN', '', time() + 86400, '/');
        } catch (\Exception $e) {
            return $this->returnJson(301, $e->getMessage());
        }
        return $this->returnJson(0, '退出成功');
    }

    /**
     *  获取账号机构列表
     */
    public function getMobileAgency(Request $request)
    {
        $mobile = trim($request->post('user_name'));
        $userType = trim($request->post('user_type'));

        try {
            $server = $this->getClient(STEAM_SERVICE_NAME, 'CommonService');
            $server->clearHeaders();
            $data = $server->getMobileAgency(['mobile' => $mobile, 'userType' => $userType]);
        } catch (\Exception $e) {
            return $this->returnJson(301, $e->getMessage());
        }
        return $this->returnJson(0, '成功', $data);
    }

    /**
     *  获取员工信息
     */
    public function getUserInfo()
    {
        try {
            $server = $this->getClient(STEAM_SERVICE_NAME,'AuthService');
            $data = $server->getUserData([]);
        }catch (\Exception $e) {
            return $this->returnJson(301,$e->getMessage());
        }

        return $this->returnJson(0,'成功',$data);
    }

    public function show(Request $request)
    {
//        return response()->json(['code' => 0, 'data' => ['name' => 'Super Admin']]);
    }

}
